package com.kh.pajx.sop.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.kh.pajx.sop.base.BaseDaoImpl;
import com.kh.pajx.sop.dao.ISysUserRoleDao;
import com.kh.pajx.sop.domain.PageBean;
import com.kh.pajx.sop.domain.SysDepartment;
import com.kh.pajx.sop.domain.SysUserRole;

/**
 * 用户角色持久化操作接口实现类
 * @author:	azzcsimp
 * @Createdate:	2014年7月17日 下午8:17:51
 */
@Repository
public class SysUserRoleDaoImpl extends BaseDaoImpl<SysUserRole> implements
		ISysUserRoleDao {

	/**
	 * 据用户组编码获取该用户组下最大角色编码
	 * @see com.kh.pajx.sop.dao.ISysUserRoleDao#getMaxUsrCode(java.lang.String)
	 */
	@Override
	public String getMaxUsrCode(String usgCode) {
		List list = findListByHql(
				"SELECT max(usr.usrCode) FROM SysUserRole usr where usr.sysUserGroup.usgCode=? ",
				usgCode);
		if (null == list.get(0)) {
			return "";
		}
		return list.get(0).toString();
	}
	
	/**
	 * 根据用户组编码获取该用户组下用户角色信息列表
	 * @see com.kh.pajx.sop.dao.ISysUserRoleDao#findUserRoleByGroupCode(java.lang.String)
	 */
	@Override
	public List<SysUserRole> findUserRoleByGroupCode(String usgCode) {
		return findListByHql("FROM SysUserRole usr where usr.sysUserGroup.usgCode=? order by usr.usrCode asc", usgCode);
	}
	
	/**
	 * 根据用户组编码统计该用户组下的用户角色数据
	 * @see com.kh.pajx.sop.dao.ISysUserRoleDao#countUserRoleByGroupCode(java.lang.String)
	 */
	@Override
	public long countUserRoleByGroupCode(String usgCode) {
		return (Long)getSession().createQuery("SELECT COUNT(*) FROM SysUserRole usr WHERE  usr.sysUserGroup.usgCode=?")
		.setParameter(0, usgCode).uniqueResult();
	}
	
	public SysUserRole getUserRoleByCode(String usrCode){
		return (SysUserRole) getSession().createQuery(//
				"FROM SysUserRole usr WHERE usr.usrCode=?")//
				.setParameter(0, usrCode)//
				.uniqueResult();
	}
}
